package com.kzd.btgame.main.home

import android.content.Context
import android.view.LayoutInflater
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.GridLayoutManager
import com.kzd.btgame.R
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.custom.setTransitionStatusE
import com.kzd.btgame.databinding.ActivityHomePresentBinding
import com.kzd.btgame.jetpack.vm.HomeViewModel
import com.kzd.btgame.main.game.adapter.GamePresentAdapter
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel

class HomePresentActivity : OwnerViewBindingActivity<ActivityHomePresentBinding>() {

    private val mGamePresentAdapter = GamePresentAdapter()

    private val mViewModel: HomeViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityHomePresentBinding
        get() = ActivityHomePresentBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flHomePresentBar
            barColor = R.color.white
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        mViewBinding.includeAppRefresh.rvAppRefreshBody.apply {
            layoutManager = GridLayoutManager(this@HomePresentActivity as Context, (2))
        }.adapter = mGamePresentAdapter

        mViewBinding.includeAppSearch.tvAppSearchSubmit.setOnClickAntiShakeListener {
            mViewModel.homePresent(
                keyword = mViewBinding.includeAppSearch.etAppSearchContent.text.toString()
            )
        }

        mViewBinding.includeAppRefresh.mSmartRefreshLayout.setOnRefreshListener {
            mViewModel.homePresent(
                keyword = mViewBinding.includeAppSearch.etAppSearchContent.text.toString()
            )
        }
        mViewBinding.includeAppRefresh.mSmartRefreshLayout.setOnLoadMoreListener {
            mViewModel.homePresent(
                keyword = mViewBinding.includeAppSearch.etAppSearchContent.text.toString(),
                isLoadMore = true
            )
        }
    }

    override fun initData() {
        mViewModel.homePresent()
    }

    override fun initViewMode() {

        mViewModel.homePresentLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatusE(response,
                mGamePresentAdapter,
                mViewBinding.includeAppRefresh.mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mGamePresentAdapter.addData(data)
            } else {
                mGamePresentAdapter.setNewInstance(data)
            }
        })
    }

}